﻿/**
* Derivative version of subModal can be downloaded from http://gabrito.com/files/subModal/
* Original By Seth Banks (webmaster at subimage dot com)  http://www.subimage.com/
* Contributions by Eric Angel, Scott, Todd Huss, Thomas Risberg, Dave Campbell
* Ported to Microsoft ASP.NET Ajax by Marnix van Valen, Alanta

Copyright (c) 2009 TallComponents BV, The Netherlands

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

*/

Type.registerNamespace('TallComponents');

TallComponents.ModalDialog = function(element) {
    TallComponents.ModalDialog.initializeBase(this, [element]);

    this._popupMask = null;
    this._popupContainer = null;
    this._returnFunc;
    this._popupIsShown = false;
    this._hideSelects = false;
    this._windowChangeDelegate = null;
    this._keypressDelegate = null;

    this._tabIndexes = new Array();
    // Pre-defined list of tags we want to disable/enable tabbing into
    this._tabbableTags = new Array("A", "BUTTON", "TEXTAREA", "INPUT", "IFRAME");
}

TallComponents.ModalDialog.prototype = {

    initialize: function() {
        TallComponents.ModalDialog.callBaseMethod(this, "initialize");

        // Hookup elements
        this._popupContainer = this.get_element();
        this._popupMask = $get(this._popupContainer.id + "_popupMask");

        // check to see if this is IE version 6 or lower. hide select boxes if so
        // maybe they'll fix this in version 7?
        var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
        this._hideSelects = (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1);

        this._windowChangeDelegate = Function.createDelegate(this, this._onWindowChange);
        $addHandler(window, 'resize', this._windowChangeDelegate);
        $addHandler(window, 'scroll', this._windowChangeDelegate);

        // If using Mozilla or Firefox, use Tab-key trap.
        if (!document.all) {
            this._keypressDelegate = Function.createDelegate(this, this._keyDownHandler);   
            $addHandler(window, 'keypress', this._keypressDelegate);
        }
    },

    dispose: function() {
        if (this._windowChangeDelagate) {
            $removeHandler(window, 'resize', this._windowChangeDelegate);
            $removeHandler(window, 'scroll', this._windowChangeDelegate);
        }
        if (this._keypressDelegate) $removeHandler(window, 'keypress', this._keypressDelegate);

        TallComponents.ModalDialog.callBaseMethod(this, 'dispose');
    },

    _onWindowChange: function(e) {
        this._centerPopWin();
    },

    // Tab key trap. if popup is shown and key was [TAB], suppress it.
    // @argument e - event - keyboard event that caused this function to be called.
    _keyDownHandler: function(e) {
        if (this._popupIsShown && e.keyCode == 9) return false;
    },

    /**
    * @argument width - int in pixels
    * @argument height - int in pixels
    * @argument url - url to display
    * @argument returnFunc - function to call when returning true from the window.
    */
    show: function(width, height, returnFunc) {
        this._popupIsShown = true;
        this._disableTabIndexes();
        this._popupMask.style.display = "block";
        this._popupContainer.style.display = "block";
        this._centerPopWin(width, height);
        //var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
        this._popupContainer.style.width = width + "px";
        this._popupContainer.style.height = height + "px";
        this._returnFunc = returnFunc;
        // for IE
        if (this._hideSelects == true) {
            this._hideSelectBoxes();
        }
    },

    /**
    * @argument callReturnFunc - bool - determines if we call the return function specified
    * @argument returnVal - anything - return value 
    */
    hide: function(result) {
        this._popupIsShown = false;
        this._restoreTabIndexes();
        if (this._popupMask == null) {
            return;
        }
        this._popupMask.style.display = "none";
        this._popupContainer.style.display = "none";
        if (this._returnFunc != null) {
        this._returnFunc(result);
        }

        // display all select boxes
        if (this._hideSelects == true) {
            this._displaySelectBoxes();
        }
    },

    _centerPopWin: function(width, height) {
        if (this._popupIsShown == true) {
            if (width == null || isNaN(width)) {
                width = this._popupContainer.offsetWidth;
            }
            if (height == null) {
                height = this._popupContainer.offsetHeight;
            }
            var size = this._getViewportSize();
            // scLeft and scTop changes by Thomas Risberg
            var scLeft, scTop;
            if (self.pageYOffset) {
                scLeft = self.pageXOffset;
                scTop = self.pageYOffset;
            } else if (document.documentElement && document.documentElement.scrollTop) {
                scLeft = document.documentElement.scrollLeft;
                scTop = document.documentElement.scrollTop;
            } else if (document.body) {
                scLeft = document.body.scrollLeft;
                scTop = document.body.scrollTop;
            }
            this._popupMask.style.height = size.height + "px";
            this._popupMask.style.width = size.width + "px";
            this._popupMask.style.top = scTop + "px";
            this._popupMask.style.left = scLeft + "px";
            var topMargin = scTop + ((size.height - (height)) / 2);
            if (topMargin < 0) { topMargin = 0; }
            this._popupContainer.style.top = topMargin + "px";
            this._popupContainer.style.left = (scLeft + ((size.width - width) / 2)) + "px";
        }
    },

    // For IE.  Go through predefined tags and disable tabbing into them.
    _disableTabIndexes: function() {
        if (document.all) {
            var i = 0;
            for (var j = 0; j < this._tabbableTags.length; j++) {
                var tagElements = document.getElementsByTagName(this._tabbableTags[j]);
                for (var k = 0; k < tagElements.length; k++) {
                    this._tabIndexes[i] = tagElements[k].tabIndex;
                    tagElements[k].tabIndex = "-1";
                    i++;
                }
            }
        }
    },

    // For IE. Restore tab-indexes.
    _restoreTabIndexes: function() {
        if (document.all) {
            var i = 0;
            for (var j = 0; j < this._tabbableTags.length; j++) {
                var tagElements = document.getElementsByTagName(this._tabbableTags[j]);
                for (var k = 0; k < tagElements.length; k++) {
                    tagElements[k].tabIndex = this._tabIndexes[i];
                    tagElements[k].tabEnabled = true;
                    i++;
                }
            }
        }
    },

    /**
    * Hides all drop down form select boxes on the screen so they do not appear above the mask layer.
    * IE has a problem with wanted select form tags to always be the topmost z-index or layer
    * Thanks for the code Scott!
    */
    _hideSelectBoxes: function() {
        for (var i = 0; i < document.forms.length; i++) {
            for (var e = 0; e < document.forms[i].length; e++) {
                if (document.forms[i].elements[e].tagName == "SELECT") {
                    document.forms[i].elements[e].style.visibility = "hidden";
                }
            }
        }
    },

    /**
    * Makes all drop down form select boxes on the screen visible so they do not reappear after the dialog is closed.
    * IE has a problem with wanted select form tags to always be the topmost z-index or layer
    */
    _displaySelectBoxes: function() {
        for (var i = 0; i < document.forms.length; i++) {
            for (var e = 0; e < document.forms[i].length; e++) {
                if (document.forms[i].elements[e].tagName == "SELECT") {
                    document.forms[i].elements[e].style.visibility = "visible";
                }
            }
        }
    },

    /**
    * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/ *
    * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
    * Gets the full width/height because it's different for most browsers.
    */
    _getViewportSize: function() {
        var size = new Object();
        if (window.innerHeight != window.undefined) { size.height = window.innerHeight; size.width = window.innerWidth; }
        else if (document.compatMode == 'CSS1Compat') { size.height = document.documentElement.clientHeight; size.width = document.documentElement.clientWidth; }
        else if (document.body) { size.height = document.body.clientHeight; size.width = document.body.clientWidth; }
        return size;
    }
}

// Optional descriptor for JSON serialization.
TallComponents.ModalDialog.descriptor = {
    properties: [{ name: "key", type: String}]
}
// Register the class as a type that inherits from Sys.UI.Control.
TallComponents.ModalDialog.registerClass('TallComponents.ModalDialog', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();